package com.hmy.cornpestdet.service;

import com.hmy.cornpestdet.mapper.UserCommonMapper;
import com.hmy.cornpestdet.pojo.User;
import com.hmy.cornpestdet.utils.CPDConst;
import com.hmy.cornpestdet.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class UserCommonService {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Autowired
    private UserCommonMapper userCommonMapper;

    private int getCurrentUid(){
        String uidStr = redisTemplate.opsForValue().get(CPDConst.CURRENT_USER_UID);
        if(uidStr == null){
            return -1;
        }
        int uid = Integer.valueOf(uidStr);
        return uid;
    }

    public Result getUserNameByUid(){
        int uid = getCurrentUid();
        if(uid == -1){
            return Result.Error("当前Uid为空");
        }
        User user = userCommonMapper.getUserNameByUid(uid);
        if(user == null){
            return Result.Error("查询失败");
        }
        return Result.Success("查询成功", user.getUsername());
    }

}
